package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysDutyPermission;
import cn.wgx.modules.sys.entity.SysDutyPermissionExample;
import cn.wgx.modules.sys.entity.mapper.SysDutyPermissionMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysDutyPermissionService extends BaseService<SysDutyPermission> {

    public static final String cacheName = "dutyPerCache";

    @Resource
    SysDutyPermissionMapper mapper;

    @Transactional(readOnly = true)
    @Cacheable(value = cacheName, unless="#result == null", key = "#root.target.getCachePreKey() + #record.toString() + #desc")
    public List<SysDutyPermission> getList(SysDutyPermission record, Boolean desc) {
		SysDutyPermissionExample example = new SysDutyPermissionExample();
		if(record != null){
            SysDutyPermissionExample.Criteria criteria = example.createCriteria();
            if(Validation.isNotNullOrZero(record.getDuty_id())){
                criteria.andDuty_idEqualTo(record.getDuty_id());
            }
            if(Validation.isNotNullOrZero(record.getPermission_id())){
                criteria.andPermission_idEqualTo(record.getPermission_id());
            }
        }
		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysDutyPermission> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysDutyPermissionExample example = new SysDutyPermissionExample();
            SysDutyPermissionExample.Criteria criteria = example.createCriteria();
            criteria.andDuty_per_idIn(uniList);
            List<SysDutyPermission> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    @Cacheable(value = cacheName, unless="#result == null", key = "#root.target.getCachePreKey() + #id")
    public SysDutyPermission getOne(Integer id) {
		return mapper.selectByPrimaryKey(id);
    }

    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public SysDutyPermission insertOne(SysDutyPermission record) {
        filter(record, true);
        mapper.insertSelective(record);
        return record;
    }


    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public SysDutyPermission updateOne( SysDutyPermission record) {
        filter(record, false);
        mapper.updateByPrimaryKeySelective(record);
        return record;

    }


    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }


    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public Integer updateRolePermission(List<SysDutyPermission> sysDutyPermissions) {
        if(sysDutyPermissions.size() > 0){
            SysDutyPermissionExample example = new SysDutyPermissionExample();
            example.createCriteria().andDuty_idEqualTo(sysDutyPermissions.get(0).getDuty_id());
            mapper.deleteByExample(example);
            int n = 0;
            for(SysDutyPermission sysDutyPermission: sysDutyPermissions){
                filter(sysDutyPermission, true);//SysDutyPermission.Column.createDate
                mapper.insertSelective(sysDutyPermission);
                n ++;
            }
//            sysDutyPermissions.get(0).SysDutyPermission.Column.excludes(SysDutyPermission.Column.duty_per_id)
//            mapper.batchInsertSelective(sysDutyPermissions,SysDutyPermission.Column.duty_id, SysDutyPermission.Column.permission_id );
            return n;
        }
        return 0;
    }
}